//
//  AdjectiveViewController.m
//  muscleLife
//
//  Created by xalo on 16/5/3.
//  Copyright © 2016年 HeavenAndHell. All rights reserved.
//

#import "AdjectiveViewController.h"
#import "AdjectiveModel.h"
#import "TableViewCell.h"
#import "DetailViewController.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHight [UIScreen mainScreen].bounds.size.height
@interface AdjectiveViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UIScrollView *titleScrollView;//标题滚动视图
@property (nonatomic,strong)UIScrollView *contentScrollView;//内容滚动视图
@property (nonatomic,strong)UILabel *lineLabe;//
@property (nonatomic,strong)NSMutableArray *titleArry;
@property (nonatomic, strong)NSMutableArray *sdCycleArry;//轮播图

@property (nonatomic,strong)NSMutableDictionary *dataSourceDic;
@property (nonatomic,strong)NSMutableArray *recommendArry;
@end

@implementation AdjectiveViewController
-(NSMutableArray *)recommendArry{
    
    if (!_recommendArry) {
        _recommendArry=[NSMutableArray array];
    }
    return _recommendArry;
}
-(NSMutableDictionary*)dataSourceDic{

    if (!_dataSourceDic) {
        _dataSourceDic=[NSMutableDictionary dictionary];
    }
    return _dataSourceDic;
}
-(NSMutableArray *)titleArry{
    if (!_titleArry) {
        _titleArry=[NSMutableArray array];
        [_titleArry addObject:@"推荐"];
    }
    return _titleArry;
}
-(NSMutableArray *)sdCycleArry{
    
    if (!_sdCycleArry) {
        _sdCycleArry=[NSMutableArray array];
    }
    return _sdCycleArry;
}
-(UIScrollView *)titleScrollView{

    if (!_titleScrollView) {
        _titleScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64,kWidth , 40)];
        [_titleScrollView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        _titleScrollView.showsHorizontalScrollIndicator=NO;
    }
    return _titleScrollView;
}
-(UILabel *)lineLabe{
    if (!_lineLabe) {
        _lineLabe=[[UILabel alloc]initWithFrame:CGRectMake(0, 37, kWidth/5, 3)];
    }
    return _lineLabe;
}
-(UIScrollView *)contentScrollView{
    
    if (!_contentScrollView) {
        _contentScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,104, kWidth, kHight)];
        
        _contentScrollView.showsVerticalScrollIndicator=NO;
//        _contentScrollView.contentSize=CGSizeMake(0, kHight+100);
    }
    return _contentScrollView;
}
//创建内容的滚动视图
-(void)CreateContentScrollView{
    
    for (int i=0; i<6; i++) {
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(i*kWidth,0, kWidth, kHight)];
     
        tableView.tag=1000+i;
        if (i==0) {
            tableView.tableHeaderView=[self CreateTableHeaderView];
        }
        tableView.delegate=self;
        tableView.dataSource=self;
        [self.contentScrollView addSubview:tableView];
    }
    self.contentScrollView.contentSize=CGSizeMake(kWidth*6, 0);
    self.contentScrollView.pagingEnabled=YES;
    self.contentScrollView.delegate=self;
 
    [self.view addSubview:self.contentScrollView];
    
}
//设置第一个tableView的页眉
-(UIView *)CreateTableHeaderView{
    
    SDCycleScrollView *sdCycleScrollView=[SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kWidth, 200) delegate:nil placeholderImage:[UIImage imageNamed:kBackGroundPlaceholderFileName]];
    [self requsetDataWithUrl:@"http://media.api.thejoyrun.com/slide-list" parameters:nil finished:^(NSDictionary *dictionary) {
        NSArray *dataArry=dictionary[@"data"];
        for (NSDictionary *object in dataArry) {
            NSString *string=object[@"cover_img"];
            [self.sdCycleArry addObject:string];
        }
        sdCycleScrollView.imageURLStringsGroup=[NSArray arrayWithArray:self.sdCycleArry];
        UITableView *tableVie=[self.view viewWithTag:1000];
        [tableVie reloadData];
    } failure:^(NSError *error) {
        
    }];
  
    return sdCycleScrollView;
}

-(void)recommendNetWork{
    //网络请求到所有标题
    [self requsetDataWithUrl:@"http://media.api.thejoyrun.com/subject-list-v1" parameters:nil finished:^(NSDictionary *dictionary) {
//        self.titleArry addObject:
        NSArray *dataArry=dictionary[@"data"];
        for (NSDictionary *object in dataArry) {
            NSString *string=object[@"subject_name"];
            [self.titleArry addObject:string];
        }
        [self CreateTitleScrollView];
        
    } failure:^(NSError *error) {
        
    }];
    //第一个界面的滚动视图
    UITableView *tabview=[self.view viewWithTag:1000];
    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@(1),@"page",@(10),@"pagesize", nil];
    [self requsetDataWithUrl:@"http://media.api.thejoyrun.com/article-list-v1" parameters:dic finished:^(NSDictionary *dictionary) {
        NSArray *dataArry=dictionary[@"data"];
        for (NSDictionary *object in dataArry) {
            AdjectiveModel *model=[[AdjectiveModel alloc]init];
            [model setValuesForKeysWithDictionary:object];
            [self.recommendArry addObject:model];
        }
        
        [tabview reloadData];
    } failure:^(NSError *error) {
        
    }];
}
//创建标题的ScrollView
-(void)CreateTitleScrollView{

    for (int i=0; i<self.titleArry.count; i++) {
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(kWidth*i/5, 0,kWidth/5, 30)];
        [btn setTitle:self.titleArry[i] forState:UIControlStateNormal];
        btn.titleLabel.font=[UIFont systemFontOfSize: 12];
        btn.tag=2000+i;
        if (i==0) {
             [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }else{
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        
        [btn addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.titleScrollView addSubview:btn];
    }
    self.lineLabe.backgroundColor=[UIColor redColor];
    [self.titleScrollView addSubview:self.lineLabe];
    self.titleScrollView.pagingEnabled=YES;
    self.titleScrollView.contentSize=CGSizeMake(kWidth+kWidth/5, 0);
    [self.view addSubview:self.titleScrollView];
}
-(void)changeView:(UIButton *)sender{
    int i=sender.frame.origin.x*5/kWidth+0.1;
//    NSLog(@"%d",i);
    self.contentScrollView.contentOffset=CGPointMake(i*kWidth, 0);
   
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView==self.contentScrollView) {
        CGPoint contentOffset=scrollView.contentOffset;
        CGRect frame=self.lineLabe.frame;
        int i=contentOffset.x/kWidth;
        frame.origin.x=kWidth/5*i;
        self.lineLabe.frame=frame;
        for (int j=0; j<6; j++) {
            UIButton *btn=[self.view viewWithTag:2000+j];
            if (j==i) {
                 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            }else{
             [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }
        
        
        if (contentOffset.x>=kWidth*4) {
            [self.titleScrollView setContentOffset:CGPointMake(kWidth/5, 0) animated:YES];
        }else {
            [self.titleScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        }
        UITableView *tabview=[self.view viewWithTag:1000+i];
        
        NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@(1),@"page",@(10),@"pagesize",@(i),@"subject_id", nil];
        [self requsetDataWithUrl:@"http://media.api.thejoyrun.com/article-list-v1" parameters:dic finished:^(NSDictionary *dictionary) {
            NSArray *dataArry=dictionary[@"data"];
            NSMutableArray *datasourceArry=[NSMutableArray array];
            for (NSDictionary *object in dataArry) {
                AdjectiveModel *model=[[AdjectiveModel alloc]init];
                [model setValuesForKeysWithDictionary:object];
                [datasourceArry addObject:model];
            }
            [self.dataSourceDic setValue:datasourceArry forKey:[NSString stringWithFormat:@"%ld",tabview.tag]];
            
            [tabview reloadData];
        } failure:^(NSError *error) {
            
        }];
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self CreateContentScrollView];
   
    for (int i=0; i<6; i++) {
        UITableView *tableView=[self.view viewWithTag:1000+i];
        [tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"TableViewCell"];
    }
  
   
    [self recommendNetWork];
    
    
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets=NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark-TableViewDelegateAndDatasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag==1000) {
        return self.recommendArry.count;
    }
    NSArray *arry=self.dataSourceDic[[NSString stringWithFormat:@"%ld",tableView.tag]];
    return arry.count;
 
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];
    AdjectiveModel *model=[[AdjectiveModel alloc]init];
    if (tableView.tag==1000) {
        model=self.recommendArry[indexPath.row];
    
    [cell setCellWithAdjectModel:model];

    }else{
        NSArray *arry=self.dataSourceDic[[NSString stringWithFormat:@"%ld",tableView.tag]];
        model=arry[indexPath.row];
        [cell settwoCellWithAdjectModel:model];
    }
    
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailViewController *detailVC=[[DetailViewController alloc]init];
    
    AdjectiveModel *model=[[AdjectiveModel alloc]init];
    if (tableView.tag==1000) {
        model=self.recommendArry[indexPath.row];
    }else{
        NSArray *arry=self.dataSourceDic[[NSString stringWithFormat:@"%ld",tableView.tag]];
        model=arry[indexPath.row];
    }
    detailVC.adModel=model;
    
    [self.navigationController pushViewController:detailVC animated:NO];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
